/**
 * @file TcpApp_client.h
 * @brief TcpApp/TCP client
 *
 * @section License
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
 *
 * This file is part of CycloneTCP Open.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * @author Oryx Embedded SARL (www.oryx-embedded.com)
 * @version 2.5.2
 **/

#ifndef _TCP_APP_CLIENT_H
#define _TCP_APP_CLIENT_H

// Dependencies
#include "core/net.h"
#include "client/tcp_client_app_common.h"

// TcpApp/TCP client support
#ifndef TCP_APP_CLIENT_SUPPORT
#define TCP_APP_CLIENT_SUPPORT ENABLED
#elif (TCP_APP_CLIENT_SUPPORT != ENABLED && TCP_APP_CLIENT_SUPPORT != DISABLED)
#error TCP_APP_CLIENT_SUPPORT parameter is not valid
#endif

// Default timeout
#ifndef TCP_APP_CLIENT_DEFAULT_TIMEOUT
#define TCP_APP_CLIENT_DEFAULT_TIMEOUT 20000
#elif (TCP_APP_CLIENT_DEFAULT_TIMEOUT < 0)
#error TCP_APP_CLIENT_DEFAULT_TIMEOUT parameter is not valid
#endif

// Forward declaration of TcpAppClientContext structure
struct _TcpAppClientContext;
#define TcpAppClientContext struct _TcpAppClientContext

// C++ guard
#ifdef __cplusplus
extern "C"
{
#endif

   /**
    * @brief TcpApp/TCP client states
    */

   typedef enum
   {
      TCP_APP_CLIENT_STATE_DISCONNECTED = 0,
      TCP_APP_CLIENT_STATE_CONNECTING = 1,
      TCP_APP_CLIENT_STATE_CONNECTED = 2,
      TCP_APP_CLIENT_STATE_SENDING = 3,
      TCP_APP_CLIENT_STATE_RECEIVING = 4,
      TCP_APP_CLIENT_STATE_COMPLETE = 5,
      TCP_APP_CLIENT_STATE_DISCONNECTING = 6
   } TcpAppClientState;

   /**
    * @brief TcpApp/TCP client context
    **/

   struct _TcpAppClientContext
   {
      TcpAppClientState state;                   ///< TcpApp/TCP client state
      NetInterface *interface;                   ///< Underlying network interface
      Socket *socket;                            ///< Underlying TCP socket
      systime_t timeout;                         ///< Timeout value
      systime_t timestamp;                       ///< Timestamp to manage timeout
      uint8_t requestAdu[TCP_APP_TCP_CLIENT_MAX_ADU_SIZE];  ///< Request ADU
      size_t requestAduLen;                      ///< Length of the request ADU, in bytes
      size_t requestAduPos;                      ///< Current position in the request ADU
      uint8_t responseAdu[TCP_APP_TCP_CLIENT_MAX_ADU_SIZE]; ///< Response ADU
      size_t responseAduLen;                     ///< Length of the response ADU, in bytes
      size_t responseAduPos;                     ///< Current position in the response ADU
   };

   // TcpApp/TCP client related functions
   error_t TcpAppClientInit(TcpAppClientContext *context);

   error_t TcpAppClientSetTimeout(TcpAppClientContext *context, systime_t timeout);

   error_t TcpAppClientBindToInterface(TcpAppClientContext *context,
                                       NetInterface *interface);

   error_t TcpAppClientConnect(TcpAppClientContext *context,
                               const IpAddr *serverIpAddr, uint16_t serverPort);

   error_t TcpAppClientUserSendData(TcpAppClientContext *context,
                                    const uint8_t *data, size_t length, bool_t resp_enable);
   
   error_t TcpAppClientReadResp(TcpAppClientContext *context, void *rdata, size_t length, size_t *length_back);

   error_t TcpAppClientDisconnect(TcpAppClientContext *context);
   error_t TcpAppClientClose(TcpAppClientContext *context);

   void TcpAppClientDeinit(TcpAppClientContext *context);

// C++ guard
#ifdef __cplusplus
}
#endif

#endif
